package com.cec.service.user.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cec.dto.ActivityDTO;
import com.cec.entity.Activity;
import com.cec.exception.DataLoadingException;
import com.cec.mapper.admin.ActivityMapper;
import com.cec.result.Result;
import com.cec.result.ResultCode;
import com.cec.service.user.UserActivityService;
import com.cec.vo.ActivityVO;
import com.cec.vo.CategoryVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 活动表 服务实现类
 * </p>
 *
 * @author lt
 * @since 2024-10-25
 */
@Slf4j
@Service
public class UserActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity> implements UserActivityService {

    @Autowired
    private ActivityMapper activityMapper;

    @Override
    public Result pageQueryActivityList(ActivityDTO activityDTO) {
        QueryWrapper<ActivityDTO> wrapper = new QueryWrapper<>();
        if (activityDTO.getActivityName() != null)
            wrapper.like("a.activity_name", activityDTO.getActivityName());
        if (activityDTO.getStatus() != null)
            wrapper.eq("a.status", activityDTO.getStatus());
        if (activityDTO.getActivityTypeId() != null)
            wrapper.eq("a.activity_type_id", activityDTO.getActivityTypeId());
        wrapper.eq("a.deleted",0)
                .eq("a.status",1)
                .orderByDesc("a.create_time");
        IPage<ActivityVO> activityVOIPage = baseMapper.pageQueryActivity(new Page<>(activityDTO.getPage(), activityDTO.getPageSize()), wrapper);
        return Result.success(activityVOIPage.getRecords(), activityVOIPage.getTotal());
    }

    @Override
    public List<CategoryVO> queryActivityType() {
        List<CategoryVO> activityCategoryVOList = baseMapper.queryActivityTypeList();
        if (activityCategoryVOList == null && activityCategoryVOList.size() <= 0)
            throw new DataLoadingException(ResultCode.DATA_LOADING_ERROR);
        return activityCategoryVOList;
    }

    @Override
    public Activity queryActivityById(Long id) {
        Activity activity = baseMapper.selectById(id);
        if (activity == null)
            throw new DataLoadingException(ResultCode.DATA_LOADING_ERROR);
        return activity;
    }
    }